package com.sl.au.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.ChanceDeliveryTemp;
import com.sl.au.entity.LogImport;
import com.sl.au.entity.OperateLog;
import com.sl.au.entity.Option;
import com.sl.au.entity.User;
import com.sl.au.service.ExcelService;
import com.sl.au.service.OperateLogService;
import com.sl.au.service.OptionService;
import com.sl.utils.DataConverter;
import com.sl.utils.ExdeUtil;

@RestController
@RequestMapping(value = "/ui/log")
public class LogController extends BaseController {

	
	@Autowired
	OperateLogService operateLogService;
	
	@Autowired
	ExcelService excelService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("log/log-index");
		return modelAndView;
	}
	/**
	 * 日志管理界面导出选项
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/exportSelects", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<org.springframework.core.io.Resource> exportSelects(HttpServletRequest request)
			throws UnsupportedEncodingException {
		//String date=request.getParameter("date");
		String beginDate = ExdeUtil.thisWeekDate().get(0);
		String endDate = ExdeUtil.thisWeekDate().get(6);
		String dept = "销售处";
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
		Date begin = DataConverter.toDate(beginDate);
		Date end = DataConverter.toDate(endDate);
		/*try {
			begin = sdf.parse(beginDate);
			end = sdf.parse(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		List<OperateLog> ol = operateLogService.getRepository().ol(begin, end, dept);//获取本周的数据
		
		List<LogImport> log = new ArrayList();
		for(int i= 0;i<ol.size();i++){
			LogImport l = new LogImport();
			Date a = ol.get(i).getUpdateTime();//获取出数据库中当前登录的时间
			String da = sdf.format(a);//将时间类型转换为字符串，方便后面比较
			if(da.equals(ExdeUtil.thisWeekDate().get(0))){
				l.setName(ol.get(i).getUser().getName());
				l.setMonday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(1))){
				l.setName(ol.get(i).getUser().getName());
				l.setTuesday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(2))){
				l.setName(ol.get(i).getUser().getName());
				l.setWednesday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(3))){
				l.setName(ol.get(i).getUser().getName());
				l.setThursday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(4))){
				l.setName(ol.get(i).getUser().getName());
				l.setFriday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(5))){
				l.setName(ol.get(i).getUser().getName());
				l.setSaturday(1);
			}
			if(da.equals(ExdeUtil.thisWeekDate().get(6))){
				l.setName(ol.get(i).getUser().getName());
				l.setSunday(1);
			}
			log.add(l);
		}
		
		return this.excelService.toExcel(log, "登录情况表.xlsx");
	}
	
    //查 询
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String startTime = request.getParameter("startTime");
    	String endTime=request.getParameter("endTime");
		Long count = null;
		List<OperateLog> logs = null;
		Map<String,String> condition=new HashMap<String,String>();
    	condition.put("startTime",startTime);
    	condition.put("endTime",endTime);
    	condition.put("key", key);
		logs=operateLogService.getLogs(start, length, condition);
		count=operateLogService.getCount(condition);
		map.put("data", logs);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	///清空日志
	@RequestMapping(value = "clear", method = RequestMethod.GET)
	public Map<String, Object> clearLog(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		operateLogService.getRepository().deleteAllInBatch();
		map.put("data", 1);
		return map;
	}
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addOperateLog", method = RequestMethod.POST)
	public Map<String, Object> addOperateLog(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account a = this.getMyAccount();
		User user = a.getUser();
		String operateType =request.getParameter("operateType");
		String content = request.getParameter("content");
		operateLogService.log(user, operateType, content);
		map.put("data", 1);
		return map;
	}
}
